package cn.com.thingo.hyintelligent.presenter;

import javax.inject.Inject;

import cn.com.thingo.hyintelligent.base.RxPresenter;
import cn.com.thingo.hyintelligent.base.contract.PersonalContract;
import cn.com.thingo.hyintelligent.model.DataManager;
import cn.com.thingo.hyintelligent.model.bean.CommonBean;
import cn.com.thingo.hyintelligent.model.bean.ModifyHeadBean;
import cn.com.thingo.hyintelligent.model.bean.UserBean;
import cn.com.thingo.hyintelligent.util.LogUtil;
import cn.com.thingo.hyintelligent.util.RxUtil;
import cn.com.thingo.hyintelligent.util.ToastUtil;
import io.reactivex.functions.Consumer;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class PersonalPresenter extends RxPresenter<PersonalContract.View> implements PersonalContract.Presenter {
    private DataManager dataManager;

    @Inject
    public PersonalPresenter(DataManager dataManager) {
        this.dataManager = dataManager;
    }

    @Override
    public void getUser(String token) {
        addSubscribe(dataManager.getUser(token)
                .compose(RxUtil.<UserBean>rxSchedulerHelper())
                .subscribe(new Consumer<UserBean>() {
                    @Override
                    public void accept(UserBean userBean) throws Exception {
                        if (userBean.getStatus() == 1) {
                            mView.viewUser(userBean);
                        } else {
                            ToastUtil.shortShow(userBean.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        LogUtil.e(throwable.getMessage());
                    }
                }));
    }

    @Override
    public void modifyHead(RequestBody token, MultipartBody.Part headPic) {
        addSubscribe(dataManager.modifyHead(token, headPic)
                .compose(RxUtil.<ModifyHeadBean>rxSchedulerHelper())
                .subscribe(new Consumer<ModifyHeadBean>() {
                    @Override
                    public void accept(ModifyHeadBean modifyHeadBean) {
                        if (modifyHeadBean.getStatus() == 1) {
                            mView.viewModifyHead(modifyHeadBean);
                        } else {
                            ToastUtil.shortShow(modifyHeadBean.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) {
                        LogUtil.e(throwable.getMessage());
                    }
                }));
    }

    @Override
    public void logout(String token) {
        addSubscribe(dataManager.logout(token)
                .compose(RxUtil.<CommonBean>rxSchedulerHelper())
                .subscribe(new Consumer<CommonBean>() {
                    @Override
                    public void accept(CommonBean commonBean) {
                        if (commonBean.getStatus() == 1) {
                            mView.viewLogout(commonBean);
                        } else {
                            ToastUtil.shortShow(commonBean.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) {
                        LogUtil.e(throwable.getMessage());
                    }
                }));
    }
}
